﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using User.Api.Controllers;


using OF.Component.Model;
using OF.Utility;
using OF.Utility.Logging;
using System.Text;
using AutoMapper;
using OF.Component.BLL;
using Swashbuckle.Swagger.Annotations;
using User.Api.Common;
using User.Api.Models;


namespace User.Api.Controllers
{
    /// <summary>
    /// 用户信息接口
    /// </summary>
    public class UserController : BaseApiController
    {
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="reqInfo">请求信息</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserAdd> Add(RequestUserAdd reqInfo)
        {
            //注册用户
            var result = ResultData<ResponseUserAdd>.Error("");
            if (!ValidateHelper.IsMobileNumber(reqInfo.UserMobile))
            {
                throw new BizException(StatusCodeEnum.UserMobileError);
            }
            var oExistedUser = UserOperate.GetUserByMobile(reqInfo.UserMobile);
            if (oExistedUser != null)
            {
                throw new BizException(StatusCodeEnum.UserMobileExisted);
            }

            //用户赋值
            var oInfo = Mapper.Map<UserInfo>(reqInfo);
            oInfo.CreatedDate = DateTime.Now;
            oInfo.UpdatedDate = DateTime.Now;
            oInfo.IsActive = 1;           

            //更新第三方来源信息
            UserSourceInfo userSourceInfo=null;
            if (!string.IsNullOrEmpty(reqInfo.SourceToken))
            {
                userSourceInfo = UserSourceOperate.GetByToken(reqInfo.SourceToken, reqInfo.SourceType);
                if (userSourceInfo != null)
                {
                    userSourceInfo.SourceType = reqInfo.SourceType;
                    //同步用户头像和昵称
                    if(!string.IsNullOrEmpty(userSourceInfo.SourceUserHead))
                    {
                        oInfo.UserName = userSourceInfo.SourceUserName;
                        oInfo.HeadImg = userSourceInfo.SourceUserHead;
                    }
                }
            }

            //添加用户并更新第三方来源信息
            var userID = UserOperate.Insert(oInfo);
            if (userSourceInfo != null)
            {
                userSourceInfo.UserID = userID;
                UserSourceOperate.Update(userSourceInfo);
            }
           
            result = ResultData<ResponseUserAdd>.Success(new ResponseUserAdd()
            {
                UserID = userID
            });
            return result;
        }

        /// <summary>
        /// 用户信息修改
        /// </summary>
        /// <param name="reqInfo">请求信息</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserUpdate> Update(RequestUserUpdate reqInfo)
        {
            var result = ResultData<ResponseUserUpdate>.Error("");
            var oExistedUser = UserOperate.GetByID(reqInfo.UserID);
            if (oExistedUser == null)
            {
                throw new BizException(StatusCodeEnum.UserNoInfor);
            }

            if (!ValidateHelper.IsMobileNumber(reqInfo.UserMobile))
            {
                throw new BizException(StatusCodeEnum.UserMobileError);
            }
            var oSameMobileUser =
                UserOperate.QuerySingle(m => m.UserID != reqInfo.UserID && m.UserMobile == reqInfo.UserMobile && m.IsActive == 1);
            if (oSameMobileUser != null)
            {
                throw new BizException(StatusCodeEnum.UserMobileExisted);
            }

            //更新操作
            Mapper.Map<RequestUserUpdate, UserInfo>(reqInfo, oExistedUser);
            oExistedUser.UpdatedDate = DateTime.Now;
            UserOperate.Update(oExistedUser);
            result = ResultData<ResponseUserUpdate>.Success(new ResponseUserUpdate()
            {
                UserID = reqInfo.UserID
            });
            return result;
        }

        /// <summary>
        /// 根据用户ID获取用户基本信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResponseUserGet> Get(long userID)
        {
            var result = ResultData<ResponseUserGet>.Error("");
            var oInfo = UserOperate.GetByID(userID);
            var oUser = Mapper.Map<ResponseUserGet>(oInfo);
            if (oInfo != null)
            {
                result = ResultData<ResponseUserGet>.Success(oUser);
            }
            else
            {
                throw new BizException(StatusCodeEnum.UserNoInfor);
            }
            return result;
        }

        /// <summary>
        /// 根据电话号码获取用户信息
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<ResponseUserGet> GetByMobile(string mobile)
        {
            var result = ResultData<ResponseUserGet>.Error("");
            var oInfo = UserOperate.GetUserByMobile(mobile);
            var oUser = Mapper.Map<ResponseUserGet>(oInfo);
            if (oInfo != null)
            {
                result = ResultData<ResponseUserGet>.Success(oUser);
            }
            else
            {
                throw new BizException(StatusCodeEnum.UserNoInfor);
            }
            return result;
        }

        /// <summary>
        /// 用户注册,如果电话号码存在则直接返回
        /// </summary>
        /// <param name="reqInfo">请求信息</param>
        /// <returns></returns>
        [HttpPost]
        public ResultData<ResponseUserAdd> AddOrGet(RequestUserAdd reqInfo)
        {
            //注册用户
            var result = ResultData<ResponseUserAdd>.Error("");
            if (!ValidateHelper.IsMobileNumber(reqInfo.UserMobile))
            {
                throw new BizException(StatusCodeEnum.UserMobileError);
            }
            var oExistedUser = UserOperate.GetUserByMobile(reqInfo.UserMobile);
            if (oExistedUser != null)
            {
                result= ResultData<ResponseUserAdd>.Success(new ResponseUserAdd()
                {
                    UserID = oExistedUser.UserID
                });
            }
            else
            {
                var oInfo = Mapper.Map<UserInfo>(reqInfo);
                oInfo.CreatedDate = DateTime.Now;
                oInfo.UpdatedDate = DateTime.Now;
                oInfo.IsActive = 1;
                var userID = UserOperate.Insert(oInfo);

               
                result = ResultData<ResponseUserAdd>.Success(new ResponseUserAdd()
                {
                    UserID = userID
                });
            }

            //更新第三方来源信息
            if (!string.IsNullOrEmpty(reqInfo.SourceToken))
            {
                var userSourceInfo = UserSourceOperate.GetByToken(reqInfo.SourceToken, reqInfo.SourceType);
                if (userSourceInfo != null)
                {
                    userSourceInfo.UserID = result.data.UserID;
                    userSourceInfo.SourceType = reqInfo.SourceType;
                    UserSourceOperate.Update(userSourceInfo);
                }
            }

            return result;
        }
    }
}
